
Components.utils.import("resource://nhnjapanpartnertoolbar/common.js")
Components.utils.import("resource://nhnjapanpartnertoolbar/moduleclickedcount.js")
Components.utils.import("resource://nhnjapanpartnertoolbar/log4moz.js")

if(typeof(NHNJapanPartnerToolbarChrome) == "undefined"){
    var NHNJapanPartnerToolbarChrome = {};
};

var toolbarLoadHandler = function(){
    NHNJapanPartnerToolbarChrome.BrowserOverylay.loadJapanToolbar();
};
var toolUnloadHandler = function(){
    NHNJapanPartnerToolbarChrome.BrowserOverylay.unloadJapanToolbar();
};
var scaleChange = function(){
    NHNJapanPartnerToolbarChrome.BrowserOverylay.scaleChangeEventHandler();

};





  
NHNJapanPartnerToolbarChrome.BrowserOverylay = {
  

  loadJapanToolbar : function(aEvent){
    

    var menuItem2 = document.getElementById("nhnjapan_partner_hello_menu_popup_item2");
    menuItem2.setAttribute("label", "new menu item name");
    
    let formatter = new Log4Moz.BasicFormatter();  
    let root = Log4Moz.repository.rootLogger;  
    let directoryService =  
        Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);   
    let localDir = directoryService.get("ProfD", Ci.nsIFile);  
    
    let logFile = localDir;
    
      
    logFile.append("log.txt");  
      
    // Loggers are hierarchical, lowering this log level will affect all  
    // output.  
    root.level = Log4Moz.Level["All"];  
      
    // this appender will log to the file system.  
    appender = new Log4Moz.RotatingFileAppender(logFile, formatter);  
    appender.level = Log4Moz.Level["All"];  
    root.addAppender(appender);
    
    nhnJsStudy.InitEnv();
  },
  
  otherWinowModify : 0,
  
  unloadJapanToolbar : function(aEvent){
    window.removeEventListener("load", toolbarLoadHandler, false);
    window.removeEventListener("unload",toolUnloadHandler, false);
    //window.alert("toolbar will be unload");
  },
  
  sayhello : function(aEvent){
    try{
     let stringBundle = document.getElementById("nhnjapanpartnertoolbar-string-bundle");
     let msg = stringBundle.getString("nhnjapanpartnertoolbar.greeting.label");
     window.alert(msg);
    }
    catch(err){
        alert(err);
    }
  },
  
  showRunTimes : function(aEvent){
    try{
        
        NHNJapanPartnerToolbarCommon.MessageCount.increment();
        let messageCount = NHNJapanPartnerToolbarCommon.MessageCount.count;
        let stringBundle = document.getElementById("nhnjapanpartnertoolbar-string-bundle");
        //let msg = stringBundle.getFormattedString("nhnjapanpartnertoolbar.click.dialog.label", [messageCount]);
        let msg = stringBundle.getFormattedString("nhnjapanpartnertoolbar.click.dialog.label", Math.sqrt(messageCount));
        window.alert(msg);
    }
    catch(err){
        alert(err);
    }
  },
  
  radioMenuClicked : function(aEvent){
    try{
        alert("radio button is clicked");
    }
    catch(err){
        alert(err);
    }
  },
  
  menuButtonGetFocus : function(aEvent){
    var menubuttonitem = document.getElementById("test-toolbar-menubutton1");
    menubuttonitem.setAttribute("label", "focused");
    
    
    alert("get focus");
    //
  },
  
  menuButtonLoseFocus : function(aEvent){
    var menubuttonitem = document.getElementById("test-toolbar-menubutton1");
    menubuttonitem.setAttribute("label", "blur");
    alert("lose focus");
  },
  
  buttonClickedMethod : function(aEvent){
    alert(aEvent.type);
    //console.debug("abc");
    //aEvent.type
    
  },
  mouseDownMethod : function(aEvent){
    aEvent.target.setAttribute("label", "down");
    //alert(aEvent.type);
    //console.debug("abc");
    //aEvent.type
    
  },  
  mouseUpMethod : function(aEvent){
    
    aEvent.target.style.color = "red";
    aEvent.target.setAttribute("label", "up");
    
    //alert(aEvent.type);
    //console.debug("abc");
    //aEvent.type
    
  },
  
  plainButtonClicked : function(aEvent){
     var myMenuButton = document.getElementById("test-toolbar-menubutton1");
     var hide = myMenuButton.getAttribute("hidden");
     if(hide == "true"){
        myMenuButton.setAttribute("hidden", "false");
     }
     else{
        myMenuButton.setAttribute("hidden", "true");
     }
  },
  
  enableButtonClicked : function(aEvent){
    let myBroadCaster = document.getElementById("nhnjapanpartnertoolbar-broadcaster");
    var disableCommand = document.getElementById("Tasks:DiableMenuButton");
    
    
    var att = disableCommand.getAttribute("disabled");
    if(att=="true"){
        disableCommand.setAttribute("disabled", "false");
        disableCommand.setAttribute("label", "menu-button");
        myBroadCaster.setAttribute("image", "chrome://nhnjapanpartnertoolbar/skin/btn_option.ico");
        myBroadCaster.setAttribute("orient", "vertical");
        myBroadCaster.setAttribute("label", "Users");
    }
    else{
        disableCommand.setAttribute("disabled", "true");
        disableCommand.setAttribute("label", "disabled menu-button");
        myBroadCaster.setAttribute("image", "");
        myBroadCaster.setAttribute("orient", "horizontal");
        myBroadCaster.setAttribute("label", "SBSB");
    }
  },
  
  yyyButtonAttChanged : function(aEvent){
    //alert("attribue is changed");
  },
  
  openWindowButtonClicked : function(aEvent){
    
    // open url
    //window.open("http://www.163.com");
    //firstwindow.xul
    
    // open dialog
    //window.openDialog("chrome://nhnjapanpartnertoolbar/content/firstwindow.xul",
    //                  "dlg",
    //                  "chrome,modal");
    //window.alert("open Dialog return");
    
    // open window
    //window.open(  
    //  "chrome://nhnjapanpartnertoolbar/content/firstwindow.xul",  
    //  "xulschoolhello-some-window",  
    //  "chrome,location,centerscreen,modal");
 
    // open alert
    //let prompts =
    //    Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService);
    //prompts.alert(window, "Alert Title", "Hello!");
        
    // confirm
    //let prompts =
    //    Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService);
    //if(prompts.confirm(window, "Confirm Title", "Would you like to continue?")){
    //    alert("ok button clicked");
    //}
    //else{
    //    alert("cancel button clicked");
    //}
    
    // window prompt
    var check = {value: false};
    let prompts =
        Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService);
    
    // prompt alert
    //var result = prompts.alert(window, "Alert Title", "Hello!");
    
    // confirm check
    //var result = prompts.confirmCheck(null, "Title of this Dialog", "Are you sure?",  
    //                              "Don't ask again", check);
    
    // alert check
    //var result = prompts.alertCheck(window, "Alert Title", "Hello!", "Check Msg", check);
    
    // prompt
    var retValue = {value:""};
    //var result = prompts.prompt(window, "Password", "Please input password",retValue,"Check Msg", check);
    //var result = prompts.prompt(window, "Password", "Please input password",retValue,null,check);
    
    // name and password
    var retValue2 = {value:""};
    //var result = prompts.promptUsernameAndPassword(window, "Password", "Please input password",retValue,retValue2,null,check);
    var items = ["hello","sb","hello"];
    var result = prompts.select(window,"Password", "Please input password", items.length, items, retValue);
    if(result){
        alert("OK " + retValue.value + retValue2.value) ;
    }
    else{
        alert("Cancel" + retValue.value + retValue2.value);
    }
    return;
    
    
    if(check.value){
        alert("checked ");
    }
    else{
        alert("unchecked");
    }
    
    //var ret;
    //var result = prompts.prompt(window,"Please input password", "Password", ret, "abc", false);
    //if(result){
    //    alert("ok button clicked with "+ret);
    //}
    //else{
    //    alert("cancel button clicked");
    //}
    
    //boolean prompt(in nsIDOMWindow aParent, in wstring aDialogTitle, in wstring aText, inout wstring aValue, in wstring aCheckMsg, inout boolean aCheckState);
    //alert("input is" + result);
    
  },
  
  myStringTestFun : function(aEvent){
    
    var len = arguments.length;
    var expectLen = arguments.callee.length;
    var s = len + " ";
    //alert(s + ", " + expectLen);
    //alert(arguments.callee.prototype);
    
    
    
    var newFun = new Function("x", "y", "var ret = x * y;return ret;");
    //alert(newFun(1,2));
    alert(newFun.toString());
    
    return;
    // transform number to string    
    {
        var myS = "hello world fdsafdsa";
        var numS = String(myS.length);
        var numS1 = myS.length + "";
        var num1 = myS.length;
        var numS2 = num1.toString(2);
        var numS3 = num1.toString(8);
        var numS4 = num1.toString(16);
        var numS5 = num1.toString(10);
        //alert("hello world(" + numS2 + ")");
        //alert("hello world(" + numS3 + ")");
        //alert("hello world(" + numS2 + ")");
        //alert("hello world(" + numS5 + ")");
    }

    // transform string to num
    {
        var myS1 = "123";
        var myS2 = "2";
        var myS3 = "123.13"
        var num1 = myS1 - 0;
        var num2 = myS2 - 0;
        var num3 = Number(myS1);
        var num4 = parseInt(myS1);
        var num5 = parseFloat(myS3);
        
        var num = num1 * num3;
        num = num4 * num5;
        
        //alert(num);
        
    }
    // Array
    {
        var c = 10;
        var myArray = new Array(10);
        for(i=0; i<c; ++i){
            myArray[i] = i;
        }
        myArray.shift();
        myArray.pop();
        myArray.splice(0,2);
        
        var numS = "";
        for(i=0; i<myArray.length; ++i){
            numS =  numS + ":" + myArray[i];
        }

        var myA = [1,2,3,4,5,6,7,8];
        //var x = myA.slice(1,3);
        var m = myA.concat([-1,-2]);
        
        alert(m.join());
        
        //alert(numS);
    }
    // Date
    {
        var  myDate = new Date();
        //alert(myDate.toDateString() + "   " + myDate.toTimeString() + "  "
        //     + myDate.toLocaleString());
    }
    
    var my_object = {
        name    :   "hello world",
        
        age     :   32,
        
        init    :   function(){
            alert("ri ben ren");
        },
    }    
    //alert("hello world(" + myS.length+")");
  },
  
  openDialogButtonClicked : function(aEvent){
    //window.showModalDialog("chrome://nhnjapanpartnertoolbar/content/firstdialog.xul",
    //                  "dlg",
    //                  "centerscreen,chrome,dialog,screenX=20,screenY=20,resizable=no,width=500,height=500");
     var dlg = window.openDialog("chrome://nhnjapanpartnertoolbar/content/firstdialog.xul",
                      "dlg",
                      "centerscreen,chrome,dialog=no,modal,screenX=20,screenY=20,resizable=no,width=500,height=700");
  },
  
  DialogOkButtonCommand : function(aEvent){
    //window.close();
    window.alert("fa");
    return;

    var bt = document.getElementById("donothing_abc");
    bt.setAttribute("buttondisabledaccept", true);
    return false;
  },
  
  DialogCancelButtonCommand : function(aEvent){
    var scaleL = document.getElementById("my-scale-label1");
     scaleL.setAttribute("value", "50");
     
    //window.close();
    return true;
  },
  
  firstDialogLoad : function(){
    //alert("load succeed");
    document.getElementById("my-scale").addEventListener("change",
                                                         scaleChange,
                                                         false);
  },
  
  scaleChangeEventHandler :function(aEvent){
    //alert("come");
    var scaleL = document.getElementById("my-scale-label1");
    var scaleC = document.getElementById("my-scale");
    var v = scaleC.getAttribute("value");
    scaleL.setAttribute("value",v);
  },
  
};


// add load event handler
window.addEventListener("load",
                        NHNJapanPartnerToolbarChrome.BrowserOverylay.loadJapanToolbar,
                        false);

// add unload event handler
window.addEventListener("unload",
                        toolUnloadHandler,
                        false)